package com.moore.controller;

import com.moore.dto.ResponseData;
import com.moore.service.LoginService;
import com.moore.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
@RequestMapping("/login")
public class loginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    // 测试
    @GetMapping("/test")
    public ResponseData Test(){
        return ResponseData.ok("success");
    }

    // 获取UUID
    @GetMapping("/UUID")
    public String getUUID() {
        System.out.println("uuid");
        return JwtUtil.getUUID();
    }

    // 获取验证码
    @GetMapping("/code")
    public int getCode(HttpServletRequest request){
        Random random = new Random();
        int code = random.nextInt(999999) + 100000;
        String uuid = request.getHeader("UUID");
        redisTemplate.opsForValue().set(uuid, String.valueOf(code));
        return code;
    }

    // 密码登录
    @GetMapping("/loginByPwd")
    public ResponseData LoginByPwd(String account, String password, HttpServletRequest request){
        return loginService.LoginByPwd(account,password,request);
    }

    // 验证码登录
    @GetMapping("/loginByCode")
    public ResponseData LoginByCode(String account, String code, HttpServletRequest request){
        return loginService.LoginByCode(account,code,request);
    }


}
